package queuedemo;

import java.util.List;

public class MyLinkQueue {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    public int usedSize;

    //尾插法
    public boolean offer(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        usedSize++;
        return true;
    }

    //删除头节点
    public int poll() {
        if (head == null) {
            return -1;
        }
        int retVal = head.val;
        if (head.next == null){
            head = null;
            last = null;
            return retVal;
        }
        head = head.next;
        head.prev = null;
        usedSize--;
        return retVal;
    }

    //获取头节点
    public int peek() {
        if (head == null) {
            return -1;
        }
        return head.val;
    }

    public boolean empty() {
        return head == null;
    }

    public int size() {
        return usedSize;
    }
}
